package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;

/**
 * Created by neo on 2016/7/7.
 */
public class GraspStateBar extends LinearLayout implements View.OnClickListener{
    /**
     * 初始化状态，未做题，不知道掌握状态
     */
    public  static  final  int GRASP_STATE_UNKNOW=0;
    /**
     * 未掌握状态
     */
    public  static  final  int GRASP_STATE_NO=1;

    /**
     * 掌握状态
     */
    public  static  final  int GRASP_STATE_YES=2;
    /**
     * 未掌握按钮
     */
    private Button video_no_grasp_btn;
    /**
     * 掌握按钮
     */
    private Button video_grasp_btn;

    private  View btn_text_analysis;

    private  View btn_video_analysis;

    private View btn_more_way_analysis;


    private  OnGraspStateChangeListener mOnGraspStateChangeListener;
    private  int mGraspState=GRASP_STATE_UNKNOW;
    private  View handle_analysis_bar;

    public GraspStateBar(Context context) {
        super(context);
        initWidget( );

    }


    public GraspStateBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWidget( );
    }

    public GraspStateBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initWidget( );
    }


    private  void  initWidget( )
    {
        LayoutInflater.from(this.getContext()).inflate(R.layout.video_btn_bottom, this);
        video_no_grasp_btn = (Button) findViewById(R.id.video_no_grasp_btn);
        video_grasp_btn = (Button) findViewById(R.id.video_grasp_btn);
        handle_analysis_bar = findViewById(R.id.handle_analysis_bar);
        video_no_grasp_btn.setOnClickListener(this);
        video_grasp_btn.setOnClickListener(this);

        btn_text_analysis = findViewById(R.id.btn_text_analysis);
        btn_video_analysis = findViewById(R.id.btn_video_analysis);
        btn_more_way_analysis = findViewById(R.id.btn_more_way_analysis);

        btn_text_analysis.setOnClickListener(this);
        btn_video_analysis.setOnClickListener(this);
        btn_more_way_analysis.setOnClickListener(this);
    }

    /**
     *
     * @param o
     * 试题的信息，用于文字解析，视频讲解跳转数据来源
     */
    public  void  setQuestionsObject(Object  o)
    {
        //等待接口确定类型

    }

    /**
     *
     * @param listener
     * 设置掌握状态监听器
     */
    public  void  setOnGraspStateChangeListener(OnGraspStateChangeListener listener)
    {
        mOnGraspStateChangeListener=listener;
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        if (R.id.video_no_grasp_btn == id) {
            //没掌握
            mGraspState=GRASP_STATE_NO;
            handle_analysis_bar.setVisibility(VISIBLE);
            if (null != mOnGraspStateChangeListener) {
                mOnGraspStateChangeListener.onGraspStateChange(GRASP_STATE_NO);
            }
            video_no_grasp_btn.setBackgroundResource(R.drawable.btn_bg_red_radius20dp);
            video_no_grasp_btn.setTextColor(getResources().getColor(R.color.white));
            setDrawableLeft(video_no_grasp_btn,R.mipmap.video_nohappy1);
            video_grasp_btn.setBackgroundResource(R.drawable.btn_bg_trans_green_border20dp);
            video_grasp_btn.setTextColor(getResources().getColor(R.color.color_light_green));
            setDrawableLeft(video_grasp_btn,R.mipmap.video_happy);

        } else if (R.id.video_grasp_btn == id) {
            //掌握

            mGraspState=GRASP_STATE_YES;
            handle_analysis_bar.setVisibility(GONE);
            video_no_grasp_btn.setBackgroundResource(R.drawable.btn_bg_withe_red_border20dp);
            video_no_grasp_btn.setTextColor(getResources().getColor(R.color.red));
            setDrawableLeft(video_no_grasp_btn,R.mipmap.video_nohappy);
            video_grasp_btn.setBackgroundResource(R.drawable.selector_btn_bg_light_green_radius20dp);
            video_grasp_btn.setTextColor(getResources().getColor(R.color.white));
            setDrawableLeft(video_grasp_btn,R.mipmap.video_happy1);

            if (null != mOnGraspStateChangeListener) {
                mOnGraspStateChangeListener.onGraspStateChange(GRASP_STATE_YES);
            }

        } else if (R.id.btn_text_analysis == id) {
            //文字解析
        }
        else if (R.id.btn_video_analysis == id) {
            //视频讲解
        }
        else if (R.id.btn_more_way_analysis == id) {
            //举一反三

        }
    }


    /**
     * @return
     * 返回掌握状态
     */
    public  int getGraspState( )
    {
        return mGraspState;
    }


    public  interface  OnGraspStateChangeListener{
          void  onGraspStateChange(int graspState);
    }

    private  void  setDrawableLeft(Button button,int resId)
    {
        Drawable drawable= getResources().getDrawable(resId);
        /// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        button.setCompoundDrawables(drawable,null,null,null);
    }
}
